package com.woniuxy.venueweb.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.lang.Snowflake;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.VenueState;
import com.woniuxy.core.util.MyJwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.util.MyOssUtil;
import com.woniuxy.venuemodel.dto.UserLookVenueDto;
import com.woniuxy.venuemodel.dto.VenueLoginDto;
import com.woniuxy.venuemodel.form.*;
import com.woniuxy.venuemodel.param.*;
import com.woniuxy.venueservice.exception.VenueException;
import com.woniuxy.venueservice.service.VenueInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 场馆表 前端控制器
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/venueInfo")
@Api(tags = "场馆详细信息接口")
public class VenueInfoController {

    @Value("${server.port}")
    private int port;

    @Autowired
    private VenueInfoService venueInfoService;

    @Resource
    private MyOssUtil myOssUtil;

    @ApiOperation("上传场馆图片或营业执照接口")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception{
        System.out.println(file);
        String type = FileTypeUtil.getType(file.getInputStream());
        if (!type.equals("jpg")&&!type.equals("png")){
            throw new VenueException(20005,"图片格式异常");
        }
        String fileName = new Snowflake(1, 1).nextIdStr();
        fileName = fileName+"."+type;
        String url = myOssUtil.uploadFile(file.getInputStream(), fileName, "ranjie11");
        return Result.success("上传图片成功",url);
    }

    @PostMapping("/register")
    @ApiOperation("场馆注册接口")
    public Result register(@RequestBody @Validated VenueRegisterForm form){
        VenueRegisterParam param = BeanUtil.toBean(form, VenueRegisterParam.class);
        venueInfoService.register(param);
        return Result.success("场馆注册成功");
    }

    @PostMapping("/login")
    @ApiOperation("场馆账号密码登录接口")
    public Result login(@RequestBody @Validated VenueLoginForm form,HttpServletResponse response, HttpServletRequest request){
        VenueLoginParam param = BeanUtil.toBean(form, VenueLoginParam.class);
        String ip = request.getRemoteAddr();
        String ipAddr = ip+":"+port;
        param.setLoginAddr(ipAddr);
        VenueLoginDto dto = venueInfoService.login(param);
        Map<String, Object> map = new HashMap<>();
        map.put("venueId",dto.getVenueId());
        String venueToken = MyJwtUtil.createVenueToken(map, 240);
        response.setHeader("token", venueToken);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.success("场馆账号密码登录成功",dto);
    }

    @PostMapping("/updateVenueImage.venue")
    @ApiOperation("修改场馆图片接口")
    public Result updateVenueImage(@RequestParam("image") MultipartFile file, @RequestHeader String token) throws IOException {
        UpdateVenueImageParam param = new UpdateVenueImageParam();
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        param.setVenueId(venueId);
        param.setImage(file);
        venueInfoService.updateVenueImage(param);
        return Result.success("修改场馆图片成功");
    }

    @PostMapping("/refine.venue")
    @ApiOperation("场馆修改简介接口")
    public Result refine(@RequestBody @Validated RefineForm form,@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        RefineParam param = BeanUtil.toBean(form, RefineParam.class);
        param.setVenueId(venueId);
        venueInfoService.refine(param);
        return Result.success("成功修改球馆个人简介信息");
    }

    @GetMapping("/checkOldPass.venue")
    @ApiOperation("验证场馆老密码接口")
    public Result checkOldPass(String oldPass, @RequestHeader String token) throws Exception {
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        venueInfoService.checkOldPass(oldPass,venueId);
        return Result.success("成功验证场馆老密码");
    }

    @PostMapping("/updatePass.venue")
    @ApiOperation("场馆修改密码接口")
    public Result updatePass(@RequestBody @Validated UpdateVenuePassForm form,@RequestHeader String token) throws Exception {
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        UpdateVenuePassParam param = BeanUtil.toBean(form, UpdateVenuePassParam.class);
        param.setVenueId(venueId);
        venueInfoService.updatePass(param);
        return Result.success("成功修改场馆密码");
    }

    @PostMapping("/updateVenueAddress.venue")
    @ApiOperation("场馆修改场馆地址接口")
    public Result updateVenueAddress(@RequestBody @Validated UpdateVenueAddressForm form,@RequestHeader String token) throws Exception {
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        UpdateVenueAddressParam param = BeanUtil.toBean(form, UpdateVenueAddressParam.class);
        param.setVenueId(venueId);
        venueInfoService.updateVenueAddress(param);
        return Result.success("成功修改场馆地址");
    }

    @PostMapping("/disableVenue.api")
    @ApiOperation("禁用场馆账号接口")
    public Result disableVenue(@RequestBody @Validated UpdateVenueStateForm form){
        venueInfoService.disableVenue(form);
        return Result.success("成功禁用场馆账号");
    }

    @PostMapping("/enableVenue.api")
    @ApiOperation("启用场馆账号接口")
    public Result enableVenue(@RequestBody @Validated UpdateVenueStateForm form){
        venueInfoService.enableVenue(form);
        return Result.success("成功启用场馆账号");
    }

    @GetMapping("/byId")
    @ApiOperation("基于场馆id查询")
    public Result<VenueLoginDto> byId(Integer venueId){
        VenueLoginDto dto = venueInfoService.byId(venueId);
        return Result.success("成功查询场馆",dto);
    }

    @GetMapping("/lookOwnerVenue.venue")
    @ApiOperation("场馆查询自己场馆信息接口")
    public Result<VenueLoginDto> lookOwnerVenue(@RequestHeader String token){
        Integer venueId = MyJwtUtil.getVenueIdByKey(token, "venueId");
        VenueLoginDto dto = venueInfoService.byId(venueId);
        return Result.success("成功查询场馆",dto);
    }
    @GetMapping("/userLookAllVenue")
    @ApiOperation("用户查看所有场馆信息")
    public Result userLookAllVenue(Integer pageNum,Integer pageSize,String address,String venueName){
        PageInfo<UserLookVenueDto> dto = venueInfoService.userLookAllVenue(pageNum,pageSize,address,venueName);
        return Result.success("成功查询所有场馆",dto);
    }

    @GetMapping("/adminLookAllVenue.api")
    @ApiOperation("平台端查看所有场馆信息")
    public Result adminLookAllVenue(Integer pageNum,Integer pageSize,Integer venueState,Integer authState,String venueName){
        PageInfo<UserLookVenueDto> dto = venueInfoService.adminLookAllVenue(pageNum,pageSize,venueState,authState,venueName);
        return Result.success("成功查询所有场馆",dto);
    }

    @GetMapping("/coachLookAllVenue.coach")
    @ApiOperation("教练查看所有已认证场馆信息")
    public Result coachLookAllVenue(Integer pageNum,Integer pageSize,String address,String venueName){
        PageInfo<UserLookVenueDto> dto = venueInfoService.userLookAllVenue(pageNum,pageSize,address,venueName);
        return Result.success("成功查询所有场馆",dto);
    }


}

